package cn.webrx.filter; /**
 * <p>Project: javaweb - ${NAME}
 * <p>Powered by webrx On 2022-03-10 09:45:21
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "AuthFilter",urlPatterns = {"/user/*","/db"})
public class AuthFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain c) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpSession session = request.getSession();
        HttpServletResponse response = (HttpServletResponse)res;
        if(session.getAttribute("user") == null){
            response.sendRedirect("/session/login.jsp");
        }
        c.doFilter(request, response);
    }
}
